package org.example.booksmanagementsystem.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.booksmanagementsystem.constant.Constants;
import org.example.booksmanagementsystem.enums.ResultStatus;
import org.example.booksmanagementsystem.model.*;
import org.example.booksmanagementsystem.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;





@Slf4j
@RestController
@RequestMapping("/user")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/login")
    public Result login(String userName, String password,String userType, HttpSession session){

//        log.info("数据传到后端了："+userName+" "+" "+ password+""+" "+userType);

        Result result = new Result();

        //校验账号密码
        if(!StringUtils.hasLength(userName)||!StringUtils.hasLength(password)){

            result.setStatus(ResultStatus.FAIL);
            return result;
        }

        UserInfo userInfo=null;

        //判断什么用户
        if("librarian".equals(userType)){

            //图书管理员
            userInfo = userInfoService.queryUserInfoByUserName(userName,password);
            if(userInfo != null){
                result.setData(userInfo);
                log.info("管理员{"+userInfo.getId()+"}登录成功");
            }

        }else if("normal".equals(userType)){

            //普通用户
            userInfo = userInfoService.queryUserInfoByUserNameNormal(userName,password);
            if(userInfo != null){
                result.setData(userInfo);

                log.info("普通用户{"+userInfo.getId()+"}登录成功");
            }

        }


        if(userInfo==null){
            result.setStatus(ResultStatus.FAIL);
            return result;
        }


        //验证成功 1.把密码去除， 2.存入session
        result.setStatus(ResultStatus.SUCCESS);

        userInfo.setPassword("");

        session.setAttribute(Constants.SESSION_USER_KEY, userInfo);

        return result;
    }

    @RequestMapping("/register")
    public Result register(UserInfo userInfo){

        Result result = new Result();

        //检验传来的参数
        if(userInfo==null){
            result.setStatus(ResultStatus.FAIL);
            result.setErrorMessage("请填写账号密码");
            return result;
        }

        result = userInfoService.registerUserInfo(userInfo);

        if(result==null){
            result.setStatus(ResultStatus.FAIL);
            result.setErrorMessage("注册失败");
            log.info("注册时，Service返回的result为空");
            return result;
        }

        result.setStatus(ResultStatus.SUCCESS);
        return result;

    }

    @RequestMapping("/getUserInfoList")
    public Result userInfoList(PageRequest pageRequest){

        // 参数校验
        if(pageRequest.getPageSize()<1||pageRequest.getCurrentPage()<=0){

            Result result = new Result();
            result.setStatus(ResultStatus.FAIL);
            result.setErrorMessage("参数验证失败");

            return result;
        }

        PageResult<UserInfo> pageResult = null;

        try{
            pageResult = userInfoService.queryNormalUserInfoList(pageRequest);
        }catch(Exception e){
            log.error("用户列表查询翻页信息错误：" + e);
        }

        return Result.success(pageResult);

    }

    @RequestMapping("getUserById")
    public UserInfo getUserById(Integer userId){
        log.info("传到后端的Id："+userId);
        if(userId==null){
            return null;
        }

        UserInfo userInfo = userInfoService.getUserInfoById(userId);

        return userInfo;
    }

    @RequestMapping("updateNormalUserInfoPassword")
    public Result updateNormalUserInfoPassword(UserInfo userInfo){
        Result result = new Result();
//        log.info("后端接收的参数："+ userInfo.toString());

        if(userInfo ==null){
            result.setStatus(ResultStatus.FAIL);
            return result;
        }

        //
        Integer n = userInfoService.updateNormalUserInfoPassword(userInfo);
        if(n==0){
            result.setStatus(ResultStatus.FAIL);
            return result;
        }

        // 密码更改成功
        result.setStatus(ResultStatus.SUCCESS);

        return result;
    }

    // 根据id删除用户--逻辑删除
    @RequestMapping("/updateUserStatus")
    public Result updateUserStatus(Integer id){
        Result result = new Result();
        if(id==null){
            result.setStatus(ResultStatus.FAIL);
            return result;
        }

        Integer n = userInfoService.updateUserStatus(id);
        if(n==0){
            result.setStatus(ResultStatus.FAIL);
            return result;
        }

        result.setStatus(ResultStatus.SUCCESS);

        return result;
    }

    // 逻辑删除大量用户
    @RequestMapping("/updateBatchUserStatus")
    public Result updateBatchUserStatus(@RequestParam("idList") List<Integer> idList){

        log.info("idList的值："+idList.toString());

        Result result = new Result();
        if(idList==null){
            result.setStatus(ResultStatus.FAIL);
            return result;
        }

        Integer n = userInfoService.updateBatchUserStatus(idList);

        if(n==0){
            result.setStatus(ResultStatus.FAIL);
            return result;
        }
        result.setStatus(ResultStatus.SUCCESS);

        return result;
    }

}

